<?php
// +----------------------------------------------------------------------
// | 微信接口
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2014
// +----------------------------------------------------------------------
// | Author: zhaojie
// +----------------------------------------------------------------------
namespace Weixin\Controller;
use Common\Controller\ShuipFCMS;

class WeixinIndexController extends ShuipFCMS{
	
	//微信验证功能
	public function index(){
		$signature = $_GET["signature"];
		$timestamp = $_GET["timestamp"];
		$nonce = $_GET["nonce"];
		$echoStr = $_GET["echostr"];
		//$pathab='./shuipf/Application/Admin/Controller/log.txt';
		if(!empty($echoStr)){
			if($this->wxChk($signature, $timestamp, $nonce)){
				echo($echoStr);
			}else{
				echo('');
			}
		}else{
			$keys = D('wp_member_public')->select();
			foreach($keys as $key=>$value){
				$key = $value['token'];	//xr//token?
				$tmpArr = array($key, $timestamp, $nonce);	//xr//token三要素
				sort($tmpArr, SORT_STRING);
				$tmpStr = implode( $tmpArr );
				$tmpStr = sha1( $tmpStr );	//xr//Secure Hash 算法 1
				if($tmpStr == $signature){
					define('THIS_TOKEN', $key);
				}
			}
			$response = new responseController();
			$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
			file_put_contents('./shuipf/Application/Weixin/Controller/aaa.txt',$postStr);
			$str=$response->responseMsg($postStr,$signature);
			file_put_contents('./shuipf/Application/Weixin/Controller/bbb.txt', $str);
			echo($str);
		}
		exit;
	}
	
	
	//微信验证
	function wxChk($signature,$timestamp,$nonce){
		$keys = D('wp_member_public')->select();
		foreach($keys as $key=>$value){
			$key = $value['token'];	//xr//token?
			$tmpArr = array($key, $timestamp, $nonce);	//xr//token三要素
			sort($tmpArr, SORT_STRING);
			$tmpStr = implode( $tmpArr );
			$tmpStr = sha1( $tmpStr );	//xr//Secure Hash 算法 1
			if($tmpStr == $signature){
				return true;
				exit;
			}else{
				return false;
			}
		}
	}
	/*
	//微信验证
	function wxChk($signature,$timestamp,$nonce){
		$key = '7bb0b7a396661286d1e37d0f0acdb9f0';	//xr//token?
		$tmpArr = array($key, $timestamp, $nonce);	//xr//token三要素
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );	//xr//Secure Hash 算法 1
		//file_put_contents('./shuipf/Application/Weixin/Controller/aaa.txt', $tmpStr.'\n'.$signature);
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}*/
}
